/**
 * 视图显示隐藏动画
 * @author slc
 */
class ViewDisplayAnim {
    _view = null


    initialDelay = null
    period = null

    onTaskRun = null

    _timerTask = new TimerTask()
    _valueAnim = new ValueAnim()

    //当前展示状态
    _currentDisplay


    constructor(view, currentDisplay = true) {
        this._view = view;
        this._currentDisplay = currentDisplay;
        //设置动画更新监听
        this._valueAnim.onUpdate = (animValue) => {
            if (this._currentDisplay) {
                this._view.style.opacity = 1 - animValue
            } else {
                this._view.style.opacity = animValue
            }
        }
        //设置动画结束监听
        this._valueAnim.onEnd = () => {
            this._currentDisplay = !this._currentDisplay
        }
        //任务执行监听
        this._timerTask.onTaskRun = (initialRun) => {
            this._valueAnim.startAnim(2048)
            if (this.onTaskRun != null) {
                this.onTaskRun.call(null, initialRun, this._currentDisplay)
            }
        }
        //任务取消监听
        this._timerTask.onTaskCancel = () => {
            this._valueAnim.cancelAnim()
            this._resetViewStatus()
        }
    }

    //开始闪烁任务
    startDelayedTaskFlash() {
        this._timerTask.startInterval(this.initialDelay, this.period)
    }

    //取消闪烁任务
    cancelDelayedTaskFlash() {
        this._timerTask.cancelInterval()
    }

    //重置视图状态
    _resetViewStatus() {
        this._view.style.opacity = 1
        this._currentDisplay = true
    }
}